if (files.cwd().endsWith("脚本")) {
    let _require = require;
    require = function (path) {
      if (path.startsWith("./")) {
        path = path.replace("./", "");
      }
      path = files.cwd() + "/gdt_hb/" + path;
      return _require(path);
    }
  }
  
  auto.waitFor();
  var utils = require("./utils.js");
  
  var storage = storages.create("xiangxiangyuedu");
  
  function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
      toastLog("检查是否有打开权限!")
      while (true) {
        var allow = text("允许").findOne(5000)
        if (allow) {
          sleep(1000)
          toastLog("发现允许授权!")
          allow.click()
          sleep(1000)
          thread.interrupt()
          break
        }
      }
    })
    sleep(1000)
    var result = launchApp(app_name)
    sleep(3000)
    thread.interrupt()
    return result
  }

function task(){
    var cnt = 0; 
    while(cnt < 3){
    var 娱乐任务 = id("entertainment").findOne(30000);
    if(娱乐任务){
        var tvadnum = id("tvadvnum").findOne(2000).text();
        log("tvadnum:" + tvadnum)
        if(tvadnum == "3/3"){
        toastLog("任务已完成");
        break ;
        }
        var finalActivity = currentActivity();
        click(娱乐任务.parent().bounds().centerX(),娱乐任务.parent().bounds().centerY())
        log("第 "+ tvadnum + " 次 视频任务")
        sleep(5000);
        toastLog("当前是广告页面.")
        utils.waitAdvert(finalActivity);
        cnt ++;
        utils.updateAppProgress(cnt * 30);
    }
 }
}

function main() {
    let open_app = launchAppEnhance("AK语音");
    if (open_app) {
      task()
    }
    else {
      toastLog("AK语音打开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");
  